﻿namespace rtgen_guiclient
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Windows.Forms;
    using System.Xml.Serialization;

    internal class ComputerList
    {
        private List<Computer> _ComputerList;
        private ListView _ListView;

        public ComputerList(ListView lv)
        {
            this._ListView = lv;
            this.Load();
        }

        public void Add(Computer Computer)
        {
            this._ComputerList.Add(Computer);
            this.AddItemToListView(Computer);
            this.Save();
        }

        private void AddItemToListView(Computer Machine)
        {
            this._ListView.Items.Add(new ListViewItem(new string[] { Machine.Hostname, Machine.Version }));
        }

        public Computer Get(string Hostname)
        {
            for (int i = 0; i < this._ComputerList.Count; i++)
            {
                if (this._ComputerList[i].Hostname == Hostname)
                {
                    return this._ComputerList[i];
                }
            }
            return null;
        }

        private void Load()
        {
            if (File.Exists("ComputerList.xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Computer>));
                FileStream stream = new FileStream("ComputerList.xml", FileMode.Open);
                this._ComputerList = (List<Computer>) serializer.Deserialize(stream);
                stream.Close();
            }
            else
            {
                this._ComputerList = new List<Computer>();
                this._ComputerList.Add(new Computer("localhost"));
            }
            this.UpdateListView();
        }

        public void Remove(Computer Computer)
        {
            this._ComputerList.Remove(Computer);
            this.RemoveItemFromListView(Computer);
            this.Save();
        }

        public void Remove(string Hostname)
        {
            for (int i = 0; i < this._ComputerList.Count; i++)
            {
                if (this._ComputerList[i].Hostname == Hostname)
                {
                    this.RemoveItemFromListView(this._ComputerList[i]);
                    this._ComputerList.RemoveAt(i);
                }
            }
            this.Save();
        }

        private void RemoveItemFromListView(Computer Machine)
        {
            for (int i = 0; i < this._ListView.Items.Count; i++)
            {
                if (this._ListView.Items[i].Text == Machine.Hostname)
                {
                    this._ListView.Items[i].Remove();
                }
            }
        }

        private void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Computer>));
            FileStream stream = new FileStream("ComputerList.xml", FileMode.Create);
            serializer.Serialize((Stream) stream, this._ComputerList);
            stream.Close();
        }

        public void SetVersion(Computer Machine, string Version)
        {
            for (int i = 0; i < this._ComputerList.Count; i++)
            {
                if (this._ComputerList[i].Hostname == Machine.Hostname)
                {
                    this._ComputerList[i].Version = Version;
                }
            }
            for (int j = 0; j < this._ListView.Items.Count; j++)
            {
                if (this._ListView.Items[j].Text == Machine.Hostname)
                {
                    this._ListView.Items[j].SubItems[1].Text = Version;
                }
            }
        }

        private void UpdateListView()
        {
            while (this._ListView.Items.Count > 0)
            {
                this._ListView.Items.RemoveAt(0);
            }
            for (int i = 0; i < this._ComputerList.Count; i++)
            {
                this.AddItemToListView(this._ComputerList[i]);
            }
        }

        public List<Computer> MachineList
        {
            get
            {
                return this._ComputerList;
            }
        }
    }
}

